package jsyf.oa.service.report.newAppReport;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.report.newAppReport.NewAppReportForGroupDao;
import jsyf.oa.dao.system.CheckDao;
import jsyf.oa.dao.system.CompanyDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.facade.report.newAppReport.NewAppReportForGroupFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@SuppressWarnings("all")
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class NewAppReportForGroupService implements NewAppReportForGroupFacade {

    @Autowired
    NewAppReportForGroupDao newAppReportForGroupDao;
    @Autowired
    private CheckDao checkDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CompanyDao companyDao;

    private String getUserIdByRoleNameAndCompanyId(String companyId, StringBuffer condition, String dateTime, Boolean flag) {
        String userIdList = "";
        List<JSONObject> userIdsList = newAppReportForGroupDao.getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, flag);
        if (null != userIdsList && userIdsList.size() > 0) {
            for (int i = 0; i < userIdsList.size(); i++) {
                userIdList += "'" + userIdsList.get(i).getString("userId") + "',";
            }
        } else {
            userIdList = "'',";
        }
        return userIdList = userIdList.substring(0, userIdList.length() - 1);
    }

    //大区经理工作报告
    @Override
    public JSONObject getBRMEmailForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");

        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='大区经理'  AND v_sys_user.MAIN=1 \n");

        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }
        String receiverIds = "";
        List<JSONObject> userList = userDao.getViewJSONListByCondition(" and id in (" + senderIds + ") and main = 1", new Object[]{});
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                receiverIds += "'" + userList.get(i).getString("parentId") + "',";
            }
        }
        if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        } else {
            receiverIds = "''";
        }


        List<JSONObject> list = newAppReportForGroupDao.getBRMEmailForGroup(senderIds, receiverIds, companyId, dateTime);
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String regionId = object.getString("regionId");
                    String userCompanyId = object.getString("companyId");
                    readerId = getReader("区域总监", regionId, userCompanyId);
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }

        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    //总经理工作报告
    @Override
    public JSONObject getZJLEmailForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='总经理'  \n");

        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);

        String receiverIds = "";

        if (!StringUtils.isBlank(senderIds) && senderIds.length() > 3) {
            List<JSONObject> userIdsList = userDao.getViewJSONListByCondition(" AND id in (" + senderIds + ") ", new Object[]{});
            if (null != userIdsList && userIdsList.size() > 0) {
                for (int i = 0; i < userIdsList.size(); i++) {
                    receiverIds += "'" + userIdsList.get(i).getString("parentId") + "',";
                }
            } else {
                receiverIds = "'',";
            }
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        }

        if (StringUtils.isBlank(receiverIds)) {
            receiverIds = "''";
        }
        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }

        List<JSONObject> list = newAppReportForGroupDao.getZJLEmailForGroup(senderIds, receiverIds, dateTime);

        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String userId = object.getString("userId");
                    readerId = userDao.getById(userId).getParentId();
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }

        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    //区域总监工作报告
    @Override
    public JSONObject getRDEmailForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");

        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='区域总监'  AND v_sys_user.MAIN=1 \n");
        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);

        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }
        String receiverIds = "";
        List<JSONObject> userList = userDao.getViewJSONListByCondition(" and id in (" + senderIds + ") and main = 1", new Object[]{});
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                receiverIds += "'" + userList.get(i).getString("parentId") + "',";
            }
        }
        if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        } else {
            receiverIds = "''";
        }


        List<JSONObject> list = newAppReportForGroupDao.getUserRegionEmailForGroup(senderIds, receiverIds, dateTime);

        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String sendId = object.getString("userId");
                    readerId = userDao.getById(sendId).getParentId();
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    //销售总监工作报告
    @Override
    public JSONObject getSDEmailForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");

        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='销售总监' AND v_sys_user.MAIN=1 \n");

        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }
        String receiverIds = "";
        List<JSONObject> userList = userDao.getViewJSONListByCondition(" and id in (" + senderIds + ") and main = 1", new Object[]{});
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                receiverIds += "'" + userList.get(i).getString("parentId") + "',";
            }
        }
        if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        } else {
            receiverIds = "''";
        }
        List<JSONObject> list = newAppReportForGroupDao.getUserRegionEmailForGroup(senderIds, receiverIds, dateTime);


        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String sendId = object.getString("userId");
                    readerId = userDao.getById(sendId).getParentId();
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    @Override
    public JSONObject getSignByUserIdAndDateTimeForTopGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        List<JSONObject> list = newAppReportForGroupDao.getSignForGroupTop(companyId, dateTime);
        JSONObject result = new JSONObject();
        result.put("records", list);

        return result;
    }

    @Override
    public JSONObject getSignByUserIdAndDateTimeForZJGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        List<JSONObject> list = newAppReportForGroupDao.getSignForGroup(companyId, dateTime, "区域总监");
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;
    }

    @Override
    public JSONObject getSignByUserIdAndDateTimeForBigRegionGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        List<JSONObject> list = newAppReportForGroupDao.getSignForGroup(companyId, dateTime, "大区经理");
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;
    }

    @Override
    public JSONObject getSignByUserIdAndDateTimeForZZGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        List<JSONObject> list = newAppReportForGroupDao.getSignForGroup(companyId, dateTime, "区域组长");
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;
    }

    @Override
    public JSONObject getSignByUserIdAndDateTimeForRegionGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        List<JSONObject> list = newAppReportForGroupDao.getSignForRegionList(companyId);
        List<JSONObject> listBigRegion = newAppReportForGroupDao.getSignForRegion(companyId, dateTime);
        JSONObject result = new JSONObject();
        result.put("records", list);
        result.put("recordsBigRegion", listBigRegion);
        return result;
    }

    //获取区域
    @Override
    public JSONObject getRegionIdByUserIdForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");

        List<JSONObject> list = newAppReportForGroupDao.getRegionIdByCompanyIdForGroup(companyId);

        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    //区域经理工作报告
    @Override
    public JSONObject getEmailByUserIdForRM(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String regionId = jsonParams.getString("regionId");
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");

        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);
        StringBuffer condition = new StringBuffer();

        //String userIds = "";

        List<JSONObject> qyList = newAppReportForGroupDao.getRmIdByRegionId(regionId, "'区域经理','区域组长'", companyId, dateTime, true);
        String senderIds = "";
        if (null != qyList) {
            for (int i = 0; i < qyList.size(); i++) {
                senderIds += "'" + qyList.get(i).getString("userId") + "',";
            }
        }
        if (!StringUtils.isBlank(senderIds) && senderIds.endsWith(",")) {
            senderIds = senderIds.substring(0, senderIds.length() - 1);
        } else {
            senderIds = "''";
        }


        List<JSONObject> dqList = newAppReportForGroupDao.getRmIdByRegionId(regionId, "'大区经理'", companyId, dateTime, false);
        String receiverIds = "";
        if (null != dqList) {
            for (int i = 0; i < dqList.size(); i++) {
                receiverIds += "'" + dqList.get(i).getString("userId") + "',";
            }
        }
        if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        } else {
            receiverIds = "''";
        }


        List<JSONObject> emilList = newAppReportForGroupDao.getEmailByUserIdAndRoleNameForRM(senderIds, receiverIds, dateTime, regionId);
        if (null != emilList && emilList.size() > 0) {
            for (int i = 0; i < emilList.size(); i++) {
                JSONObject object = emilList.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String userRegionId = object.getString("regionId");
                    String userCompanyId = object.getString("companyId");
                    readerId = getReader("大区经理", userRegionId, userCompanyId);
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("records", emilList);
        return result;

    }

    @Override
    public JSONObject getTodayPlanByUserIdAndDateTimeForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        String roleName = jsonParams.getString("roleName");
        String regionId = "";
        if (jsonParams.containsKey("regionId")) {
            regionId = jsonParams.getString("regionId");

        }
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName in ( '" + roleName + "') and main=1 \n");

        String userIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        if (!StringUtils.isBlank(regionId)) {
            List<JSONObject> qyList = newAppReportForGroupDao.getRmIdByRegionId(regionId, "'区域经理','区域组长'", companyId, dateTime, true);
            String receiverIds = "";
            if (null != qyList && qyList.size() > 0) {
                for (int i = 0; i < qyList.size(); i++) {
                    receiverIds += "'" + qyList.get(i).getString("userId") + "',";
                }
            } else {
                userIds = "''";
            }
            if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
                userIds = receiverIds.substring(0, receiverIds.length() - 1);
            }
        }
        List<JSONObject> records = newAppReportForGroupDao.getTodayPlanByUserIdAndDateTimeForGroup(userIds, dateTime);
        JSONObject result = new JSONObject();
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getBRMTodayFirstVisitAndSignForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        StringBuffer condition = new StringBuffer();
        condition.append(" and roleName='大区经理'\n");
        String userIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        List<JSONObject> firstVisitAndSignList = newAppReportForGroupDao.getFirstVisitAndSignForToday(userIds, dateTime);
        JSONObject result = new JSONObject();
        result.put("records", firstVisitAndSignList);
        return result;
    }

    @Override
    public JSONObject getTodayFirstVisitAndSignForBRM(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        StringBuffer condition = new StringBuffer();
        condition.append(" and roleName in ('区域经理','区域组长')\n");
        String userIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        List<JSONObject> firstVisitAndSignList = newAppReportForGroupDao.getFirstVisitAndSignForToday(userIds, dateTime);
        JSONObject result = new JSONObject();
        result.put("records", firstVisitAndSignList);
        return result;
    }

    @Override
    public JSONObject getBRMVisitByUserIdAndDateTimeForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String states = jsonParams.getString("states");//0-AM/1-PM
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName in ('大区经理') \n");
        String userIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);

        List<JSONObject> jsonObjects = newAppReportForGroupDao.getForPersonalDayByUserIdAndDateTime(userIds, dateTime, Integer.valueOf(states));
        JSONObject result = new JSONObject();
        result.put("records", jsonObjects);
        return result;
    }

    //区域经理今日面电访
    @Override
    public JSONObject getRMVisitByUserIdAndDateTimeForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String regionId = jsonParams.getString("regionId");
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        String states = jsonParams.getString("states");//0-AM/1-PM
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        StringBuffer condition = new StringBuffer();
        /*condition.append(" AND roleName in ('区域经理','区域组长') \n");

        String userIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);*/

        List<JSONObject> qyList = newAppReportForGroupDao.getRmIdByRegionId(regionId, "'区域经理','区域组长'", companyId, dateTime, false);
        String userIds = "";
        if (null != qyList) {
            for (int i = 0; i < qyList.size(); i++) {
                userIds += "'" + qyList.get(i).getString("userId") + "',";
            }
        }
        if (!StringUtils.isBlank(userIds) && userIds.endsWith(",")) {
            userIds = userIds.substring(0, userIds.length() - 1);
        }

        List<JSONObject> jsonObjects = newAppReportForGroupDao.getForPersonalDayByUserIdAndDateTime(userIds, dateTime, Integer.valueOf(states));
        JSONObject result = new JSONObject();
        result.put("records", jsonObjects);
        return result;

    }

    //区域支持工作报告
    @Override
    public JSONObject getRSEmailForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);
        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='区域支持'  AND v_sys_user.MAIN=1 \n");

        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }
        String receiverIds = "";
        List<JSONObject> userList = userDao.getViewJSONListByCondition(" and id in (" + senderIds + ") and main = 1", new Object[]{});
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                receiverIds += "'" + userList.get(i).getString("parentId") + "',";
            }
        }
        if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        } else {
            receiverIds = "''";
        }
        List<JSONObject> list = newAppReportForGroupDao.getUserRegionEmailForGroup(senderIds, receiverIds, dateTime);

        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String regionId = object.getString("regionId");
                    String userCompanyId = object.getString("companyId");
                    readerId = getSupportReader("销售管理", regionId, userCompanyId);
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }

        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    //销售管理工作报告
    @Override
    public JSONObject getXMEmailForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='销售管理'  AND v_sys_user.MAIN=1 \n");

        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }

        String receiverIds = "";
        List<JSONObject> userList = userDao.getViewJSONListByCondition(" and id in (" + senderIds + ") and main = 1", new Object[]{});
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                receiverIds += "'" + userList.get(i).getString("parentId") + "',";
            }
        }
        if (!StringUtils.isBlank(receiverIds) && receiverIds.endsWith(",")) {
            receiverIds = receiverIds.substring(0, receiverIds.length() - 1);
        } else {
            receiverIds = "''";
        }

        List<JSONObject> list = newAppReportForGroupDao.getZJLEmailForGroup(senderIds, receiverIds, dateTime);

        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String userId = object.getString("userId");
                    readerId = userDao.getById(userId).getParentId();
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }

        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    //区域组长工作报告
    @Override
    public JSONObject getEmailByUserIdForRTL(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");

        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }
        dateTime = DateUtils.getyesterdayByDateTime(dateTime);

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName ='区域组长'  AND v_sys_user.MAIN=1 \n");

        String senderIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        condition.delete(0, condition.length());
        condition.append(" AND roleName ='大区经理' \n");
        String receiverIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);

        if (StringUtils.isBlank(receiverIds)) {
            receiverIds = "''";
        }
        if (StringUtils.isBlank(senderIds)) {
            senderIds = "''";
        }
        List<JSONObject> list = newAppReportForGroupDao.getRTLEmailForZSQ(senderIds, receiverIds, dateTime);

        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject object = list.get(i);
                String readerId = object.getString("readerId");
                if (StringUtils.isBlank(readerId)) {
                    String regionId = object.getString("regionId");
                    List<JSONObject> dqList = newAppReportForGroupDao.getUserIdByRegionId(regionId, "大区经理", receiverIds);
                    if (null != dqList && dqList.size() > 0) {
                        readerId = dqList.get(0).getString("userId");
                    }
                    if (!StringUtils.isBlank(readerId)) {
                        object.put("readerId", readerId);
                        object.put("readerName", userDao.getById(readerId).getName());
                    }
                    if ("未发送".equals(object.getString("titleSend"))) {
                        Integer count = newAppReportForGroupDao.getSendEmail(object.getString("userId"), dateTime);
                        if (null != count && count != 0) {
                            object.put("titleSend", "已发送");
                        }
                    }
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("records", list);
        return result;

    }

    @Override
    public JSONObject getCostByUserIdAndDateTimeForGroup(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        if (StringUtils.isBlank(dateTime)) {
            Calendar c = Calendar.getInstance();
            dateTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        }

        StringBuffer condition = new StringBuffer();
        condition.append(" AND roleName in ('总经理','销售总监','区域总监','大区经理','区域组长','区域经理')\n");

        String userIds = getUserIdByRoleNameAndCompanyId(companyId, condition, dateTime, false);
        List<JSONObject> personalCostList = newAppReportForGroupDao.getForPersonalCostByUserIdAndDateTime(userIds, dateTime);
        JSONObject result = new JSONObject();
        result.put("records", personalCostList);
        return result;
    }

    private String getSupportReader(String roleName, String regionId, String companyId) {
        List<String> roleNameList = new ArrayList();
        Map<String, Integer> roleNameMap = new HashMap<>();

        roleNameList.add("销售管理");
        roleNameList.add("区域支持");

        roleNameMap.put("销售管理", 0);
        roleNameMap.put("区域支持", 1);

        StringBuffer roleNames = new StringBuffer();
        int listIndex = 0;
        Integer index = roleNameMap.get(roleName);
        if (null != index) {
            listIndex = roleNameMap.get(roleName);
        } else {
            listIndex = -1;
            roleNames.append("'" + roleName + "'");
        }
        for (int i = listIndex; i >= 0; i--) {
            roleNames.append("'" + roleNameList.get(i) + "'");
            if (i != 0) {
                roleNames.append(",");
            }
        }
        if (roleNames.length() <= 0) {
            roleNames.append("''");
        }
        List<JSONObject> list = newAppReportForGroupDao.getNoUserRegionReader(regionId, roleNames.toString(), companyId);
        if (null != list && list.size() > 0) {
            return list.get(0).getString("userId");
        }
        return "";
    }

    private String getReader(String roleName, String regionId, String companyId) {
        List<String> roleNameList = new ArrayList();
        Map<String, Integer> roleNameMap = new HashMap<>();

        roleNameList.add("总经理");
        roleNameList.add("销售总监");
        roleNameList.add("区域总监");
        roleNameList.add("大区经理");
        roleNameList.add("区域组长");
        roleNameList.add("区域经理");
        roleNameMap.put("总经理", 0);
        roleNameMap.put("销售总监", 1);
        roleNameMap.put("区域总监", 2);
        roleNameMap.put("大区经理", 3);
        roleNameMap.put("区域组长", 4);
        roleNameMap.put("区域经理", 5);

        StringBuffer roleNames = new StringBuffer();
        int listIndex = 0;
        Integer index = roleNameMap.get(roleName);
        if (null != index) {
            listIndex = roleNameMap.get(roleName);
        } else {
            listIndex = -1;
            roleNames.append("'" + roleName + "'");
        }
        for (int i = listIndex; i >= 0; i--) {
            roleNames.append("'" + roleNameList.get(i) + "'");
            if (i != 0) {
                roleNames.append(",");
            }
        }
        if (roleNames.length() <= 0) {
            roleNames.append("''");
        }
        List<JSONObject> list = newAppReportForGroupDao.getNoUserRegionReader(regionId, roleNames.toString(), companyId);
        if (null != list && list.size() > 0) {
            return list.get(0).getString("userId");
        }
        return "";
    }
}